package com.framework2.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.framework.Utils.Utils;
import com.framework2.base.BaseOnClickListener;
import com.asiainfo.heshanghui.R;

/**
 * 退出dialog
 *
 * @author Yangjie
 *         className ExistDialog
 *         created at  2017/4/18  16:07
 */
public class ExitDialog extends AlertDialog {
    private View view;
    private TextView tvSure;
    private TextView tvCancel;
    private TextView tv_content;

    public ExitDialog(Activity activity) {
        super(activity);
        init();
    }

    public ExitDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    public ExitDialog(Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        init();
    }

    private void init() {
        view = LayoutInflater.from(getContext()).inflate(
                R.layout.dialog_exist, null);
        tv_content = (TextView) view.findViewById(R.id.tv_content);
        tvSure = (TextView) view.findViewById(R.id.tv_sure);
        tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
//        view.setPadding(32, 0, 32, 0);
    }

    private void initData() {
        tvCancel.setOnClickListener(new BaseOnClickListener() {
            @Override
            public void onBaseClick(View v) {
                dismiss();
            }
        });
        tvSure.setOnClickListener(new BaseOnClickListener() {
            @Override
            public void onBaseClick(View v) {

                if (null != onclickListener) {
                    onclickListener.onSureClickListener("");
                }
            }
        });
    }

    public ExitDialog setContent(@NonNull String content) {
        tv_content.setText(content);
        return this;
    }

    /**
     * 显示dialog
     */
    public void showDialog() {
        initData();
        show();
        Window window = getWindow();
        if (null != window) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = Utils.getScreenWidth(getContext());
            window.setAttributes(layoutParams);
            window.setGravity(Gravity.CENTER);

            window.setBackgroundDrawable(new ColorDrawable(0));
            window.setContentView(view);
            setCancelable(true);
            setCanceledOnTouchOutside(false);
        }
    }


    private OnSureClickListener onclickListener;

    public ExitDialog setOnSureClickListener(OnSureClickListener onclickListener) {
        this.onclickListener = onclickListener;
        return this;
    }

    public interface OnSureClickListener {
        void onSureClickListener(String info);
    }
}